package qujian;

import until.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class PickupPanel extends JPanel {
    private JTextField codeField;

    public PickupPanel() {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);

        JLabel titleLabel = new JLabel("扫码取件", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 28));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(30, 10, 20, 10));
        add(titleLabel, BorderLayout.NORTH);

        JPanel contentPanel = new JPanel(new GridBagLayout());
        contentPanel.setBackground(Color.WHITE);
        add(contentPanel, BorderLayout.CENTER);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 10, 15, 10);
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 取件码 Label
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel codeLabel = new JLabel("请输入取件码：");
        codeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        contentPanel.add(codeLabel, gbc);

        // 取件码输入框
        gbc.gridx = 1;
        codeField = new JTextField(15);
        codeField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        codeField.setPreferredSize(new Dimension(250, 35));
        contentPanel.add(codeField, gbc);

        // 使用取件码按钮
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        JButton pickupBtn = new JButton("使用取件码取件");
        pickupBtn.setBackground(new Color(76, 175, 80)); // 绿色
        pickupBtn.setForeground(Color.WHITE);
        pickupBtn.setFocusPainted(false);
        pickupBtn.setFont(new Font("微软雅黑", Font.BOLD, 18));
        pickupBtn.setPreferredSize(new Dimension(260, 40));
        pickupBtn.addActionListener(e -> handlePickup());
        contentPanel.add(pickupBtn, gbc);

        // 模拟人脸识别按钮
        gbc.gridy = 2;
        JButton faceBtn = new JButton("模拟人脸识别取件");
        faceBtn.setBackground(new Color(100, 149, 237)); // 蓝色
        faceBtn.setForeground(Color.WHITE);
        faceBtn.setFocusPainted(false);
        faceBtn.setFont(new Font("微软雅黑", Font.BOLD, 18));
        faceBtn.setPreferredSize(new Dimension(260, 40));
        faceBtn.addActionListener(e -> showFaceRecognitionDialog());
        contentPanel.add(faceBtn, gbc);
    }

    private void handlePickup() {
        String code = codeField.getText().trim();

        if (code.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入取件码！", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try (Connection conn = DBUtil.getConnection()) {
            // 查询是否存在对应包裹
            String query = "SELECT id, locker_id, status FROM parcels WHERE code = ?";
            PreparedStatement pstmt = conn.prepareStatement(query);
            pstmt.setString(1, code);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                int parcelId = rs.getInt("id");
                int lockerId = rs.getInt("locker_id");
                String status = rs.getString("status");

                if ("已取".equals(status)) {
                    JOptionPane.showMessageDialog(this, "该包裹已被取走！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 更新包裹状态为“已取”
                String updateParcel = "UPDATE parcels SET status = '已取', pickup_time = NOW() WHERE id = ?";
                PreparedStatement updateParcelStmt = conn.prepareStatement(updateParcel);
                updateParcelStmt.setInt(1, parcelId);
                updateParcelStmt.executeUpdate();

                // 更新柜门状态为空闲
                String updateLocker = "UPDATE lockers SET status = '空闲' WHERE id = ?";
                PreparedStatement updateLockerStmt = conn.prepareStatement(updateLocker);
                updateLockerStmt.setInt(1, lockerId);
                updateLockerStmt.executeUpdate();

                JOptionPane.showMessageDialog(this, "取件成功，柜门已打开！", "成功", JOptionPane.INFORMATION_MESSAGE);
                codeField.setText("");
            } else {
                JOptionPane.showMessageDialog(this, "取件码无效或包裹不存在！", "错误", JOptionPane.ERROR_MESSAGE);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库操作失败！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showFaceRecognitionDialog() {
        JDialog dialog = new JDialog((Frame) SwingUtilities.getWindowAncestor(this), "人脸识别", true);
        dialog.setSize(400, 200);
        dialog.setLocationRelativeTo(this);
        dialog.setLayout(new BorderLayout());

        JLabel label = new JLabel("模拟人脸识别成功，柜门已打开！", SwingConstants.CENTER);
        label.setFont(new Font("微软雅黑", Font.BOLD, 18));
        label.setBorder(BorderFactory.createEmptyBorder(20, 10, 10, 10));
        dialog.add(label, BorderLayout.CENTER);

        JButton okButton = new JButton("确认");
        okButton.setBackground(new Color(66, 133, 244));
        okButton.setForeground(Color.WHITE);
        okButton.setFocusPainted(false);
        okButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        okButton.setPreferredSize(new Dimension(100, 40));
        okButton.addActionListener(e -> dialog.dispose());

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        dialog.add(buttonPanel, BorderLayout.SOUTH);

        dialog.setVisible(true);
    }
}
